let answersApp = new Vue({
    el:'#answersApp',
    data:{
        answers:[
        ]
    },
    methods:{
        loadAnswers:function (){
            //地址栏问号后面的值
            let id = location.search;
            if(!id){
                alert("非法访问!参数不足！");
                location.href='/index.html';
            }
            id = id.substring(1);
            if (!id || isNaN(id)) { // is not a number
                alert("非法访问！参数不足！");
                location.href = '/index.html';
                return;
            }
            $.ajax({
                url:'/api/v1/answers/question/' + id,
                success:function (json) {
                    let answers = json.data;
                    for (let i = 0; i < answers.length; i++) {
                        answers[i].createdTimeText = getCreatedTimeText(answers[i].createdTime);
                    }
                    answersApp.answers = answers;
                }
            });
        },
        postAnswer:function () {
            let questionId = location.search.substring(1);
            let content = $('#summernote').val();
            //注意:以下data表示提交到服务器端的数据，属性名称必须与AnswerDTO的属性名称保持|一 致
            let data = {
                questionId:questionId,
                content:content
            }
            $.ajax({
                url:'/api/v1/answers/post',
                data:data,
                type:'post',
                success:function (json) {
                    if(json.state ==2000){
                        alert("回复成功");
                        //应该将数据显示到列表
                        //如果要上传图片，必领启动静态资源服务器
                        //$('#form-post-answer')[0].reset();
                        $('#summernote').summernote('reset');
                        // 获取服务器端返回的新回答案的数据

                        let answer = json.data;
                        answer.createdTimeText = getCreatedTimeText(answer.createdTime);
                        //answer.comments=[];
                        // unshift()：在数组顶部添加元素
                        answersApp.answers.unshift(answer);
                        // 切到当前页面的“回答列表”顶部
                        location.href = "#answerListTop";
                    }else{
                        alert(json.message);
                    }
                }
            });
        },
        postComment:function (answerId) {
            let content = $('#commentContent'+ answerId).val();
            //alert("准备发表答案id=" + answerId + "的评论,评论内容是：" + content);
            let data={
                answerId:answerId,
                content:content
            }
            $.ajax({
                url: '/api/v1/comments/post',
                data: data ,
                type: 'post',
                success:function (json) {
                    if(json.state ==2000){
                        //alert("发表成功！");
                        //从服务器端返回的数据中获取评论数据对象
                        let comment = json.data;
                        //由于当前页面的数据包含多条“回答” 需要先找到本次评论对应的回答
                        //则遍历整个answers（即所有的回答）检查id与参数answerId是否相同
                        for(let i=0;i<answersApp.answers.length;i++){
                            if (answersApp.answers[i].id == answerId){
                                answersApp.answers[i].comments.unshift(comment);
                                break;
                            }
                        }
                        location.href= '#answer' + answerId;
                    }else{
                        alert(json.message);
                    }
                }
            });
        },
        deleteComment:function (comments,index) {
            let commentId = comments[index].id;
            let confirmMessage = '即将删除id=' + commentId + "的评论,删除将不可回复，您确定是否要删除" ;
            //弹窗内容是确认还是去校
            if(!confirm(confirmMessage)){
                return;
            }
            alert("删除，即将删除");
            $.ajax({
                url:'/api/v1/comments/'+commentId+'/delete',
                type:'post',
                success:function (json) {
                    if(json.state==2000){
                        //alert("删除成功！");
                        //在指定位置删除
                        //参数1：开始的位置  参数二：删几个
                        comments.splice(index,1);
                    }else{
                        alert(json.message);
                    }
                }
            });

        },
        updateComment:function (comments,index) {
            let commentId = comments[index].id;
            let content = $('#CommentContent'+commentId).val();
            //alert("即将修改id="+commentId+"的内容,内容为：" + content);
            let data = {
                content:content
            }
            $.ajax({
                url:'/api/v1/comments/'+commentId+'/update',
                data:data,
                type:'post',
                success:function (json) {
                    if(json.state==2000){
                       // alert("修改成功！");
                       // comments[index] = json.data; //错误
                        //unshift、shift、splice这几个可以
                        Vue.set(comments,index,json.data);
                        $('#updateComment'+commentId).collapse('hide');
                    }else{
                        alert(json.message);
                    }

                }
            });
        }
    },
    created:function () {
        this.loadAnswers();
    }
});